PHP XML Parsing Functions

    These PHP functions will allow the XML parsing which is a PHP extension. These functions enable us to manipulate the XML data and to get the parsed data. This function gets enabled by default and you can disable then from the default command line at the compile time. This extension will work without any configuration directive defined in the php.ini file.

    Functions

    Sutf8_decode

    This function will allow you to do the conversion from a string with ISO-8859-1 characters which is encoded with UTF-8 to single-byte ISO-8859-1 Syntax

    string utf8_decode ( string $data )

    Utf8_encode

    This function will allow you to encode an ISO-8859-1 string to UTF-8. Syntax

    string utf8_encode ( string $data )

    Xml_error_string

    This function will provide you with the XML parser error string with a short error message. Syntax

    string xml_error_string ( int $code ) )

    Xml_get_current_byte_index

    This function will allow you to provide the current byte index for a provided XML parser Syntax

    int xml_get_current_byte_index ( resource $parser )

    Xml_get_current_column_number

    This function will allow you to provide the current column number for a provided XML parser syntax

    int xml_get_current_column_number ( resource $parser )

    Xml_get_current_line_number

    This function will allow you to provide the current line number for a provided XML parser Syntax

    xml_get_current_line_number(parser)

    Xml_get_error_code

    This function will provide you with the error code for the given XML parser Syntax

    int xml_get_error_code ( resource $parser )

    Xml_parse_into_struct

    This function will allow you to parse any formatted XML into an array structure for the given parser. Syntax

    int xml_parse_into_struct ( resource $parser , string $data , array &$values [, array &$index ] )

    Example

    <?php
       $var1 = "<para><note>data</note></para>";
       $var2 = xml_parser_create();
       xml_parse_into_struct($var2, $var1, $vals, $index);
       xml_parser_free($var2);  
       print_r($index);
       echo "/n";
       print_r($vals);
    ?>

    Output

    Array ( [PARA] => Array ( [0] => 0 [1] => 2 ) [NOTE] => Array ( [0] => 1 ) ) /nArray ( [0] => Array ( [tag] => PARA [type] => open [level] => 1 ) [1] => Array ( [tag] => NOTE [type] => complete [level] => 2 [value] => data ) [2] => Array ( [tag] => PARA [type] => close [level] => 1 ) )

    Xml_parser_create_ns

    This function will allow you to create an XML parser along with namespace support Syntax

    resource xml_parser_create_ns ([ string $encoding [, string $separator = ":" ]] )

    Xml_parser_create

    This function will allow you to create an XML parser Syntax

    xml_parser_create(encoding)

    Example-

    <?php
       $data = xml_parser_create();
       xml_parser_free($data);
    ?>

    Xml_parser_free

    This function will allow you to free an XML parser Syntax

    bool xml_parser_free ( resource $parser )

    Example-

    <?php
       $data = xml_parser_create();
       xml_parser_free($data);
    ?>

    Xml_parser_get_option

    This function will allow you to get the options from a given XML parser Syntax

    xml_parser_get_option(parser,option,value)

    Example

    <?php
       $data = xml_parser_create();
       echo xml_parser_get_option($data, XML_OPTION_CASE_FOLDING);
       xml_parser_free($data);
    ?>

    Output

    1

    Xml_parser_set_option

    This function will allow you to set options in a given XML parser Syntax

    xml_parser_set_option(parser,option,value)

    Example

    <?php
       $data = xml_parser_create();
       xml_parser_set_option($data, XML_OPTION_SKIP_WHITE, 1);
       xml_parser_free($data);
    ?>

    Xml_set_character_data_handler

    This function will allow you to set up character data handler for a given XML parser. Syntax

    bool xml_set_character_data_handler ( resource $parser , callable $handler )

    Xml_set_default_handler

    This function will allow you to set up default handler for a given XML parser. Syntax

    xml_set_default_handler(parser,handler)

    Example Below is the XML code

    <?xml version = "1.0" encoding = "UTF-8"?>
    <note>
       <to>Jacob</to>
       <from>SAM</from>
       <heading>Subject</heading>
       <body>Friday sale!</body>
    </note>

    Below is the php code

    <?php
       $data = xml_parser_create();
       function default($data,var){
          echo $var
       }
       xml_set_default_handler($data,"default");
       $fp = fopen("demo.xml","w");
       while ($data=fread($fp,4096)) {
          xml_parse($data,$data,feof($fp)) or 
          die (sprintf("XML Error: %s at line %d", 
          xml_error_string(xml_get_error_code($data)),
          xml_get_current_line_number($data)));
       }
       xml_parser_free($data);
    ?>

    Output-

    Jacob Sam Friday sale!

    Xml_set_element_handler

    This function will allow you to set up start and end element handlers for a given XML parser. syntax

    xml_set_element_handler(parser,start,end)

    Xml_set_end_namespace_decl_handler

    This function will allow you to set up end namespace declaration handler for a given XML parser. Syntax

    bool xml_set_end_namespace_decl_handler ( resource $parser , callable $handler )

    Xml_set_external_entity_ref_handler

    This function will allow you to set up an external entity reference handler for a given parser. Syntax

    bool xml_set_external_entity_ref_handler ( resource $parser , callable $handler )

    Xml_set_notation_decl_handler

    This function will allow you to set up notation declaration handler for a given XML parser. Syntax

    bool xml_set_notation_decl_handler ( resource $parser , callable $handler )

    Xml_set_object

    This function will allow you to specify the XML Parser within an object Syntax

    bool xml_set_object ( resource $parser , object &$object )

    Xml_set_processing_instruction_handler

    This function will allow you to set up processing instruction (PI) handler for a given XML parser. Syntax

    bool xml_set_processing_instruction_handler ( resource $parser , callable $handler )

    Xml_set_start_namespace_decl_handler

    This function will allow you to set up start namespace declaration handler for a given XML parser. Syntax

    bool xml_set_start_namespace_decl_handler ( resource $parser , callable $handler )

    Xml_set_unparsed_entity_decl_handler

    This function will allow you to set up the unparsed entity declaration handler for a given XML parser. Syntax

    bool xml_set_unparsed_entity_decl_handler ( resource $parser , callable $handler )

    People are also reading: